import { FeatureCheckerService } from '@abp/features/feature-checker.service';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Component, OnInit, Injector, EventEmitter, Output } from '@angular/core';
import { FormGroup, Validators, FormBuilder } from '@angular/forms';
import { AreaDTO, ServiceProxy } from '@shared/service-proxy/service-proxies';

@Component({
  selector: 'app-create-area-modal',
  templateUrl: './create-area-modal.component.html',
  styleUrls: ['./create-area-modal.component.less']
})
export class CreateAreaModalComponent extends AppComponentBase implements OnInit {
  @Output() modalSave = new EventEmitter<any>();
  validateForm: FormGroup;
  checkStatusList = [];
  area: AreaDTO = new AreaDTO();
  isVisible = false;
  companyList = [];
  isOkLoading = false;

  constructor(injector: Injector,
    private fb: FormBuilder,
    private _service: ServiceProxy) {
    super(injector)
  }

  ngOnInit() {
    this.validateForm = this.fb.group({
      code: [null, [Validators.required]],
      // siteId: [null, [Validators.required]],
      warehouseId: [null, [Validators.nullValidator]],
      latitude: [null, [Validators.required]],
      longitude: [null, [Validators.required]],
      radius: [null, [Validators.required]]
    });

    this.getAllWarehouse();
  }

  getAllWarehouse() {
    this._service.getAllWarehouseDTOsUsingGET().subscribe(result => {
      this.companyList = result.result.fieldList;
    })
  }

  handleOk(): void {
    this.isOkLoading = true;
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[i].markAsDirty();
      this.validateForm.controls[i].updateValueAndValidity();
    }

    this.checkStatusList.forEach(m => {
      if (m.checked == true) {
        if (m.label == this.l('sealing_containers_area')) {
          this.area.blockFlag = true;
        }
        if (m.label == this.l('deblocking_area')) {
          this.area.unblockFlag = true;
        }
      }
      else {
        if (m.label == this.l('sealing_containers_area')) {
          this.area.blockFlag = false;
        }
        if (m.label == this.l('deblocking_area')) {
          this.area.unblockFlag = false;
        }
      }
    })
    if(!this.area.unblockFlag&&!this.area.blockFlag)
    {
      this.isOkLoading = false;
      this.message.error(this.l('verify_failed_please_check_the_form_item'));
      return;
    }
    if (this.validateForm.valid) { // 验证通过

      this._service.createAreaUsingPOST(this.area).subscribe(result => {
        this.isOkLoading = false;
        if (result.success) {
          this.message.success(this.l('save_success'));
          this.handleCancel()
          this.modalSave.emit();
        } else {
          this.message.info(this.l(result.error.message));
        }

      }, error => {
        this.isOkLoading = false;
        this.message.error(this.l(error.message));
      })

    } else { // 验证未通过
      this.isOkLoading = false;
      this.message.error(this.l('verify_failed_please_check_the_form_item'));
    }
  }

  handleCancel(): void {
    console.log('Button cancel clicked!');
    this.isVisible = false;
  }

  show(id?: string) {
    this.isVisible = true;
    if (id) {
      this._service.getAreaUsingGET(id).subscribe(result => {
        this.area = result.result;
        this.checkStatusList = [
          { label: this.l('sealing_containers_area'), value: this.area.blockFlag, checked: this.area.blockFlag },
          { label: this.l('deblocking_area'), value: this.area.unblockFlag, checked: this.area.unblockFlag }];
      })
    }
    else {
      this.area = new AreaDTO();
    }
  }
}
